package com.zhongyu.scheduler.provider.apiimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhongyu.comm.api.SchedulerSeatApi;
import com.zhongyu.comm.api.SchedulerSeatPriceApi;
import com.zhongyu.comm.pojo.SchedulerSeat;
import com.zhongyu.comm.pojo.SchedulerSeatPrice;
import com.zhongyu.comm.pojo.Task;
import com.zhongyu.comm.util.Constants;
import com.zhongyu.comm.util.JacksonUtil;
import com.zhongyu.comm.util.SnowflakeIdWorker;
import com.zhongyu.comm.vo.CreateOrderMessage;
import com.zhongyu.comm.vo.CreateOrderVo;
import com.zhongyu.scheduler.provider.mapper.SchedulerSeatMapper;
import com.zhongyu.scheduler.provider.mapper.SchedulerSeatPriceMapper;
import com.zhongyu.scheduler.provider.mapper.TaskMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SchedulerSeatApiImpl implements SchedulerSeatApi {

    @Resource
    private SchedulerSeatMapper schedulerSeatMapper;

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private SchedulerSeatPriceMapper schedulerSeatPriceMapper;

    @Override
    public List<SchedulerSeat> findByScheduleId(Integer scheduleId) {
        QueryWrapper<SchedulerSeat> queryWrapper = Wrappers.query();
        queryWrapper.eq("schedule_id", scheduleId);
        return schedulerSeatMapper.selectList(queryWrapper);
    }

    /**
     * 修改座位状态
     *锁
     * @return
     */
    @Transactional
    public String updateSeatAndAddTask(CreateOrderVo createOrderVo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        String orderNo = idWorker.nextId() + "";
        String seatPosition = createOrderVo.getSeatPositions();//x1_y1,x2_y2
        String[] arr01 = seatPosition.split(",");
        double sum=0;
        String perPrice="";
        for (int i=0;i<arr01.length;i++) {
            String seatStr=arr01[i];
            String[] arr02 = seatStr.split("_");
            int x = Integer.parseInt(arr02[0]);
            int y = Integer.parseInt(arr02[1]);

        QueryWrapper<SchedulerSeat> queryWrapper = Wrappers.query();
        queryWrapper.eq("x", x);
        queryWrapper.eq("y", y);
        queryWrapper.eq("schedule_id", createOrderVo.getSchedulerId());
        SchedulerSeat schedulerSeat = schedulerSeatMapper.selectOne(queryWrapper);
       // schedulerSeat.setOrderNo(orderNo);
        schedulerSeat.setStatus(2);//2锁定付款状态
        schedulerSeat.setUpdatedTime(LocalDateTime.now());
        schedulerSeat.setUserId(createOrderVo.getUserId());
        schedulerSeatMapper.updateById(schedulerSeat);
            QueryWrapper<SchedulerSeatPrice> wrapper=Wrappers.query();
            wrapper.eq("schedule_id",schedulerSeat.getScheduleId());
            wrapper.eq("area_level",schedulerSeat.getAreaLevel());
            SchedulerSeatPrice schedulerSeatPrice=  schedulerSeatPriceMapper.selectOne(wrapper);
         double price=   schedulerSeatPrice.getPrice();
            if(i==0){
                perPrice=""+price;
            }else{
                perPrice=perPrice+","+price;
            }
         sum+=price;
    }
    if (createOrderVo.getIsNeedInsurance()==1){
        sum+=20;
    }
        CreateOrderMessage createOrderMessage=new CreateOrderMessage();
         createOrderMessage.setAmount(sum);
         createOrderMessage.setOrderNo(orderNo);
         createOrderMessage.setPerPrice(perPrice);
        Task task=new Task();
        task.setCreateTime(LocalDateTime.now());
        task.setMqExchange(Constants.RabbitExchangeName.ORDER_CREATE_EXCHANGE);
        task.setMqRoutingkey(Constants.RabbitRoutingKey.ORDER_CREATE);
        task.setOvertime(LocalDateTime.now().plusMinutes(1));


        BeanUtils.copyProperties(createOrderVo,createOrderMessage);
        String json= JacksonUtil.obj2json(createOrderMessage);
        task.setRequestBody(json);
        System.out.println("json"+json);
        task.setStatus(0);
        task.setTryCount(0);
        task.setVersion(1);
        task.setTaskType("创建订单Task");
        taskMapper.insert(task);
      /*  Map<String,Object> map=new HashMap<>();
        map.put("status","true");
        map.put("task",task);*/
        return orderNo;
    }


}
